<template>
    <div class="main_box" style="padding:10px">
      <div class="add_wrap">
        <el-row>
        <div class="tit">
          <h2>{{ dataForm.promoteModelId ? '编辑推广模板' : '新增推广模板' }}</h2>
          <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
        </div>
        <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="rules">
          <el-row>
            <el-form-item prop="modelName" label="推广模板名称">
              <el-input  v-model="dataForm.modelName"></el-input>
            </el-form-item>
            <el-form-item prop="modelType" label="推广模板类型">
              <el-select v-model="dataForm.modelType" clearable>
                <el-option  :value="1" label="服务" key="1">
                </el-option>
                <el-option  :value="2" label="平台" key="2">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="remark" label="备注">
              <el-input type="textarea"  v-model="dataForm.remark"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
        <div class="tit">
          <h2>商户推广规则明细</h2>
        </div>
        <el-table ref="tableList" :data="promoteRuleList" @selection-change="handleSelectChange">
          <el-table-column type="selection" align="center">

          </el-table-column>
          <el-table-column prop="businessName" label="商户名称" align="center">

          </el-table-column>
          <el-table-column prop="ruleName" label="推广规则名称" align="center">

          </el-table-column>
          <el-table-column label="推广规则类型" align="center">
              <template slot-scope="scope">
                  {{ scope.row.ruleType==1?'商户':'平台' }}
              </template>
          </el-table-column>
        </el-table>
      </el-row>
          <el-row>
            <el-form-item>
              <el-button plain type="primary" @click="sureClick">保 存</el-button>
              <el-button plain @click="returnList">取 消</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-row>

      </div>
    </div>
  </template>

  <script>
  import * as message from "@/constants/message";
  import PromoteModelApi from '@/api/operation/promoteModel';
  import PromoteRuleApi from "@/api/operation/promoteRule"
  export default {
    data() {
      return {
        dataForm: {
          promoteModelId:"",
          modelName:"",
          modelType: 1,
          list:[],
          remark: "",
        },
        promoteRuleList:[],
        rules: {
          modelName: [
            { required: true, message: "请输入推广模板名称", trigger: "change" }
          ],
          modelType: [
            { required: true, message: "请选择推广模板类型", trigger: "change" }
          ],
        }
      }
    },
    components: {
    },
    mounted() {
      this.init()
    },
    methods: {
      async init() {
        await this.getPromoteRuleList()
        if (this.$route.query.id) {
          this.dataForm.promoteModelId = this.$route.query.id
          this.getData()
        }
      },
      keydown(e) {
        if (e.keyCode == 32) {
          e.returnValue = false;
        }
      },
      getData() {
        PromoteModelApi.getPromoteModel({
          id: this.dataForm.promoteModelId
        }).then(res => {
          this.$Utils.bindingBean(this.dataForm, res.data)
          this.selectRow()
        })
      },
      selectRow(){
        this.dataForm.list.forEach(item=>{
          this.promoteRuleList.find(item2=>{
            if(item2.promoteRuleId == item.promoteRuleId){
              this.$refs.tableList.toggleRowSelection(item2,true)
              return true
            }
          })
        })
      },
      async getPromoteRuleList(){
          let res = await PromoteRuleApi.promoteRuleList()
          this.promoteRuleList = res.data
      },
      handleSelectChange(val){
        this.dataForm.list = val.map(item=>({promoteRuleId:item.promoteRuleId,relationId:item.relationId||""}))
      },
      checkData(){
        if(this.dataForm.list.length==0){
          this.$message.error("请至少选择一条商户推广规则关联")
          return false
        }

        return true
      },
      sureClick() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
              .then(() => {
                if(!this.checkData()) return
                if (this.dataForm.promoteModelId) {
                  this.editData();
                } else {
                  this.addData();
                }
              });
          }
        })
      },
      addData() {
        let param = {
            modelName: this.dataForm.modelName,
            modelType: this.dataForm.modelType,
            list:this.dataForm.list,
            remark: this.dataForm.remark,
        };

        PromoteModelApi.addPromoteModel(param).then(res => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
      },
      editData() {
        let param = {
          promoteModelId: this.dataForm.promoteModelId,
          modelName: this.dataForm.modelName,
          modelType: this.dataForm.modelType,
          list:this.dataForm.list,
          remark: this.dataForm.remark,
        };
        PromoteModelApi.editPromoteModel(param)
          .then(() => {
            this.$Utils.notifySuccess()
            this.returnList();
          })
      },
      returnList() {
        this.$store.dispatch('delView', this.$route);
        this.$router.push({ path: '/operation/promoteModelManagement' });
      }
    },

  }
  </script>
  <style lang="scss">
  @import "@/assets/css/views/add.scss";
  @import "@/assets/css/views/content.scss";
  </style>

  <style lang="scss" scoped>
  .upload-img {
    &::v-deep .el-upload-dragger {
      display: flex;
      justify-content: space-around;
      align-items: center;

      .tip {
        padding-bottom: 30px;
      }
    }
  }
  </style>

